package com.cloudweb.oa.utils;

import cn.js.fan.util.StrUtil;
import com.cloudweb.oa.entity.UserSetup;
import com.cloudweb.oa.service.IUserSetupService;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.oa.sys.DebugUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
public class UniPush2Util {

    @Autowired
    private RestTemplate restTemplate;

    // uni-push 云函数 URL
    @Value("${uni.push.url}")
    private String pushUrl;

    public void push(String userName, String title, String content) {
        if ("".equals(userName)) {
            return;
        }

        // 过滤掉content中的html代码
        content = StrUtil.getAbstract(null, content, 100, "", false);

        IUserSetupService userSetupService = SpringUtil.getBean(IUserSetupService.class);
        UserSetup userSetup = userSetupService.getUserSetup(userName);
        if (userSetup == null) {
            return;
        }

        if (null == userSetup.getCid()) {
            DebugUtil.e(GtPushUtil.class, userName, "cid is null");
            return;
        }

        sendPushMessage(userSetup.getCid(), "0", title, content);
    }

    /**
     *
     * @param pushClientid
     * @param type 自定义的标识字段
     * @param title
     * @param content
     * @return
     */
    public String sendPushMessage(String pushClientid, String type, String title, String content) {
        // 构建请求体
        Map<String, Object> pushData = new HashMap<>();
        pushData.put("push_clientid", pushClientid);
        pushData.put("title", title);
        pushData.put("content", content);
        // 以上三个为必传字段
        Map<String, Object> payloadData = new HashMap<> ();
        payloadData.put("type", type);
        pushData.put("payload",payloadData);  // 可选的额外数据
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json");

        // 构建请求实体
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(pushData, headers);

        // 发送 HTTP POST 请求
        try {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

            ResponseEntity<String> response = restTemplate.exchange(pushUrl, HttpMethod.POST, entity, String.class);
            return response.getBody();
        } catch (Exception e) {
            LogUtil.getLog(getClass()).error(e);
            return "推送失败";
        }
    }
}
